package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntContactForeignSignMapper;
import com.prd.model.EntContactForeignCheck;
import com.prd.model.EntContactForeignSign;
import com.prd.model.LoginUser;
import com.prd.service.EntContactForeignSignService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntContactForeignSignServiceImpl extends ServiceImpl<EntContactForeignSignMapper, EntContactForeignSign> implements EntContactForeignSignService {

    @Autowired
    private EntContactForeignSignMapper entContactForeignSignMapper;

    @Override
    public int insertInfo(EntContactForeignSign entContactForeignSign) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntContactForeignSign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntContactForeignSign::getForeignId, entContactForeignSign.getForeignId());
        queryWrapper.eq(EntContactForeignSign::getAcceptorId, entContactForeignSign.getAcceptorId());
        EntContactForeignSign sign = entContactForeignSignMapper.selectOne(queryWrapper);
        if (sign != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entContactForeignSign);
        return entContactForeignSignMapper.insert(entContactForeignSign);
    }

    @Override
    public int updateInfo(EntContactForeignSign entContactForeignSign) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entContactForeignSign);
        return entContactForeignSignMapper.updateById(entContactForeignSign);
    }
}
